<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <meta name="keywords" content=""/>
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/app.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/config.js"></script>
</head>
<body>

<nav id="navbar" class="navbar has-shadow">
    <div class="container">
        <div id="navMenu" class="navbar-menu">
            <div class="navbar-start">
                <a class="navbar-item" href="index.html">主页</a>
                <a class="navbar-item" href="about.html">关于</a>
            </div>
            <div class="navbar-end">
                <div class="navbar-item">
                    <a class="button is-small is-primary" href="register.html">注册</a>
                </div>
                <div class="navbar-item">
                    <a class="button is-small" href="login.html">登录</a>
                </div>
            </div>
        </div>
    </div>
</nav>


<div class="container">
    <div class="box sign-box">
        <div>
            <div class="field">
                <label class="label">用户名</label>
                <div class="control">
                    <input class="input" id="username" type="text" autocomplete="off" placeholder="请输入用户名">
                </div>
            </div>
            <div class="field">
                <label class="label">密码</label>
                <div class="control">
                    <!-- 禁止自动填充用户名密码 -->
                    <input type="password" style="position: absolute;left: 0;display: none"/>
                    <input class="input" id="password" type="password" placeholder="请输入密码">
                </div>
            </div>
            <div class="field is-grouped">
                <div class="control">
                    <button class="button is-primary" onclick="doLogin()">立即登录</button>
                </div>
                <div class="control">
                    <a href="register.html" class="button is-text">还没账号？</a>
                </div>
            </div>
        </div>
    </div>
</div>
<script>

    // 登录方法
    function doLogin() {
        // 1.非空效验
        var username = jQuery("#username");
        var password = jQuery("#password");
        var code=jQuery("#imgText");
        if (username.val().trim() == "") {
            alert("请先输入用户名！");
            username.focus();
            return false;
        }
        if (password.val().trim() == "") {
            alert("请先输入密码！");
            password.focus();
            return false;
        }
        if(code.val().trim()=="")
        {
            alert("请先输入验证码！");
            code.focus();
            return false;
        }
        // 2.进行登录操作
        jQuery.ajax({
            url: req_base_url + "user/login",
            method: "POST",
            data: {
                "username": username.val(),
                "password": password.val(),
                "code":code.val()
            },
            success: function (res) {
                // 3.将执行的结果展示给用户
                if (res != null && res.code == 200) {
                    // 登录成功
                    // 将服务器端生成的 token 保存起来
                    window.localStorage.setItem(token_key,
                        res.data.token);
                    console.log(window.localStorage.getItem(token_key));
                    window.localStorage.setItem("uid",
                        res.data.uid);
                    window.localStorage.setItem("manager",
                        res.data.manager);
                    window.localStorage.setItem("username",
                        res.data.username);
                    alert("恭喜：登录成功！");
                    location.href = "index.html";
                } else {
                    // 登录失败
                    alert("抱歉：登录失败！" + res.msg);
                }
            }
        });
    }
</script>
</body>
</html>
